

export default {
	state: {
		
		webset:{
			price:0,
			dan:0
		},
		select: {
			type:"纯文字",
			zz:"企业",
			sbname:"",
	     	img:"",
			title:"",
			address:"",
			code:"",
			mobile:"",
			lxr:"",
			sb_select:[]
		}
	},
	getters: {
		
        second: (state, getters) => (id) => {
			let res=state.select.sb_select.filter(todo => todo.second == id)
            return res.length
         },
		 // price:(state,getters)=>{
			// console.log(state.webset.price,1)
			// return state.webset.price 
		 // },
		 // dan:(state)=>{
		 // 			return state.webset.dan
		 // },
		 first_price: (state, getters) => (id) => {
			let num= getters.first(id)
			let price=parseInt(state.webset.price)
			let dan=parseInt(state.webset.dan)
			
			
			let total= num>10?price+dan*(num-10):price
			
			if (state.select.type=="文字图片双注册")
			    total=total*2
				
			return total
			
		 },
		 first: (state, getters) => (id) => {

		 	let res=state.select.sb_select.filter(todo => todo.first == id)
			
		     return res.length
		  },
		  total: (state) => { 
			
			  return state.select.sb_select.length
		  },
		  total_price: (state,getters) => { 
			  let res=getters.first_total
			  let total=0
			  res.map(v=>{
				  total=total+v.price
			  })
			 
			  return total?total:0
		  },
		  first_total:(state,getters)=>{
			  let res=[];
			  state.select.sb_select.map(function(v){
                   let res1=res.find(v1 => v1.first == v.first)
				   if (!res1){
					   res.push(v)
				   }
              })
			  res.sort(function(a,b){
			      return a.id-b.id
			  })
			  res.map(v=>{
				  
				   v['num']=getters.first(v.first)
				   v['price']=getters.first_price(v.first)
				   return v
			  })
			  return res;
			  
		  }
    
	},
	mutations: {
		// 覆盖收货地址
		change_select(state, set) {
			state.select[set.field]=set.value;
			
			uni.setStorageSync("select",state.select)	
		},
		set_select(state, select) {
			state.select=select
			uni.setStorageSync("select",state.select)	
			console.log(state.select)
		},
		set(state, webset) {
			state.webset = webset
			
			console.log(state)
		}
         

	},
	actions: {
		// 修改地址
		doset(context,$u) {
			$u.post('set', {}).then(res => {
				
				context.commit('set', res)
			});
		}
	},
}
